$(function () {

    //日历交互
    lectureDateFn();
    getLiveTime();
    //日历交互
    function lectureDateFn() {
        var _flag = false,
            _bo = $('body');

        _bo.on('click', '.lectureDateBtn', function () {
            getLiveTime();
            if (_flag) {
                _bo.removeClass('lecture-open');
                _flag = false
            } else {
                _bo.addClass('lecture-open');
                _flag = true
            }
            //区域刷新 重置滚动条
            $('.infinite-scroll').scroller('refresh');
        }).on('click', '.classSchedule-baRow > a', function () {
            var that = $(this),
                aId = that.data('id');
            that.addClass('active').siblings().removeClass('active');
            if(aId === 'month') {
                _bo.addClass('lecture-open');
                _flag = true
            }else{
                _bo.removeClass('lecture-open');
                _flag = false
            }
            //区域刷新 重置滚动条
            $('.infinite-scroll').scroller('refresh');
            getLiveTime();
        }).on('click','.picker-calendar',function () {
            getLiveTime();
        });;

        //初始化日历，选择日期并触发
        $('section.lecture-date-bx').calendar({
            onChange: function (o, timestamps, date) {
                loadDateClassCourse( new Date(date));
            }
        })
    }
});

function getLiveTime() {
    $.ajax({
        url: baseloaction + '/mobile/uc/ajax/findLiveTime',
        dataType: 'json',
        type: 'post',
        success: function (response) {
            if (response.success) {
                var entity = response.entity;
                if (entity != null) {
                    $('.picker-calendar-month-current .picker-calendar-day').each(function (i, v) {
                        var year = JSON.stringify($(v).data('year'));
                        var month = JSON.stringify($(v).data('month'));
                        var day = JSON.stringify($(v).data('day'));
                        if (parseInt(month) < 9) {
                            month = '0' + (parseInt(month) + 1);
                        } else {
                            month = (parseInt(month) + 1);
                        }
                        if (day.length < 2) {
                            day = '0' + day;
                        }
                        var str = year + '-' + month + '-' + day;
                        if(entity.indexOf(str) > -1){
                            $(v).addClass('active').html('<span>课</span>');
                        }
                    });
                }
            }
        },
        error: function () {
        }
    })
}